import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * 收集答案
 *
 * @author Li Sipeng
 * @version 4.1 on   2022-11-20 14:20
 */
public class Answers {
    private ArrayList<Integer> list = new ArrayList<>();  //存放输入的答案

    /**
     * 打印错题集*/
    public void printErrorOperations(ArrayList list,int NUM_PER_LINE,int type) throws IOException {
        System.out.println("错题如下：");
        OperationBase ob = new OperationBase() ;
        ob.formatAndDisplayExercise(list,NUM_PER_LINE,type); //打印算式，一行5个
    }

    /**
     *打印结果统计
     *
     *
     **/
    public void printResult(HashMap map){
        System.out.println("*************\t题量：" + map.get("total")+
                "\t正确率："+ map.get("Accuracy")+
                "\t错题数："+map.get("falseNum")+"\t*************");
    }

    /**
     * 接收答案
     *
     * */
    public void collectAnswers(ArrayList<Operation> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("\n请输入算式结果：\n");
        Operation e;
        int m,n,o,v;
        for (Operation item: list ) {
                e = item;
                m = e.getLeft_operand();
                n = e.getRight_operand();
                o = e.getOperator();
                if(o == '+') {  //加法算式,操作符：0--加法；1--减法
                        System.out.printf("%2d + %2d =     ", m, n);
                }else {
                        System.out.printf("%2d - %2d =     ", m, n);
                }
            int inputValue = 0;
            try {
                inputValue = sc.nextInt();
                item.setInputValue(inputValue);
            } catch (Exception ex) {
                System.out.println("要输入数字奥！");
                Scanner sc2 = new Scanner(System.in);
                inputValue = sc2.nextInt();
                item.setInputValue(inputValue);
                continue;
            }
        }

    }

    /**
     * 判断答案对错
     *
     * @param list 传入的算式Operation的ArrayList
     * @return 返回一个HashMap trueNum正确数量，falseNum错误数量， Accuracy正确率，total题目总数，errorList错误集合
     * */
    public HashMap checkAnswers(ArrayList<Operation> list){
        int listSize = list.size();
        ArrayList<Operation> errorList = new ArrayList<>();
        HashMap map = new HashMap();
        int trueNum = 0,falseNum = 0;
        for (int i = 0; i < listSize; i++) {
            if (list.get(i).getInputValue()==list.get(i).getValue()){
                trueNum++;
            }else{
                falseNum++;
                errorList.add(list.get(i));
            }
        }
        double Accuracy = ((double) trueNum /listSize)*100;
        map.put("trueNum",trueNum);
        map.put("falseNum",falseNum);
        map.put("Accuracy",Accuracy+"%");
        map.put("total",listSize);
        map.put("errorList",errorList);
        return map;
    }


    public ArrayList<Integer> getList() {
        return list;
    }

    public void setList(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Answers{" +
                "list=" + list +
                '}';
    }



}
